#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import check_command_injection
from common.exception.common_exception import ErrCodeException
from informix import LOGGER
from informix.comm.comm import check_path_user, identify_env_user
from informix.comm.const import JsonConstant, RegisterType, ErrorCode, InformixInfo


class ResourceBaseParam:
    """
    连通性检查与资源接入入参解析基类
    """

    def __init__(self, param_dict):
        self._body_param = param_dict
        self.application = self._body_param.get(JsonConstant.APPLICATION, {})
        self.applications = self._body_param.get(JsonConstant.APPLICATIONS, [])
        self.extend_info = self.application.get(JsonConstant.EXTEND_INFO, {})
        self.appenv = self.get_appenv()
        self.onconfig_path = ""
        self.sqlhosts_path = ""
        self.agent_ip_list_curr, self.agent_ip_list_bound = "", ""
        self.host_uuid_curr, self.host_uuid_bound = "", ""
        self.is_cluster = self._get_single_or_cluster() == RegisterType.CLUSTER
        self.db_user = identify_env_user() if identify_env_user() else InformixInfo.INFORMIX_USER

    def get_sub_type(self):
        para = self.applications[0].get(JsonConstant.SUB_TYPE)
        return para

    def get_appenv(self):
        appenv = self._body_param.get(JsonConstant.APP_ENV, "")
        return appenv

    def check_if_cluster(self):
        return self.is_cluster

    def get_agent_ip_list_curr(self):
        self.agent_ip_list_curr = self.appenv.get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.AGENT_IP_LIST, "")
        return self.agent_ip_list_curr

    def get_agent_ip_list_bound(self):
        return self.agent_ip_list_bound

    def get_host_uuid_curr(self):
        return self.host_uuid_curr

    def get_host_uuid_bound(self):
        return self.host_uuid_bound

    def get_application(self):
        self.application = self._body_param.get(JsonConstant.APPLICATION, {})
        return self.application

    def get_extend_info(self):
        self.extend_info = self.application.get(JsonConstant.EXTEND_INFO, {})
        return self.extend_info

    def get_appenv_extend_info(self):
        return self.appenv.get(JsonConstant.EXTEND_INFO, {})

    def get_single_instance_name(self):
        if not self.applications:
            return ""
        return self.applications[0].get(JsonConstant.NAME, "")

    def get_single_sqlhost(self):
        if not self.applications:
            return ""
        single_sqlhost = self.applications[0].get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.SQLHOSTS_PATH, "")
        if single_sqlhost:
            if check_command_injection(single_sqlhost) or not check_path_user(single_sqlhost,
                                                                              self.db_user, self.db_user):
                LOGGER.error("Failed to verify the parameter(single_sqlhost) validity.")
                raise ErrCodeException(ErrorCode.ERROR_PARAM.value)
        return single_sqlhost

    def get_single_onconfig(self):
        if not self.applications:
            return ""
        single_onconfig = self.applications[0].get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.ONCONFIG_PATH, "")
        if single_onconfig:
            if check_command_injection(single_onconfig) or not check_path_user(single_onconfig,
                                                                               self.db_user, self.db_user):
                LOGGER.error("Failed to verify the parameter(single_onconfig) validity.")
                raise ErrCodeException(ErrorCode.ERROR_PARAM.value)
        return single_onconfig

    def get_open_log(self):
        return self.application.get(JsonConstant.EXTEND_INFO, {}).get(JsonConstant.LOG_BACKUP_KEY, "")

    def _get_single_or_cluster(self):
        para = self.applications[0].get(JsonConstant.SUB_TYPE)
        if para == JsonConstant.INFORMIX_SINGLE_INSTANCE:
            return RegisterType.SINGLE
        elif para == JsonConstant.INFORMIX_CLUSTER_INSTANCE:
            return RegisterType.CLUSTER
        return ''
